# -*- coding: utf-8 -*-
import copy
import json
import os

import scrapy
from scrapy import Request

from ShopifyMonitor.items import ShopifyItem


class ShopifySpider(scrapy.Spider):
    name = 'shopify'

    def start_requests(self):
        """
        API： http://xxxx.com/collections/all/products.json
        :return:
        """
        filename = os.path.join('doc', 'shopify-sites.txt')
        with open(filename) as f:
            for line in f:
                base_url = line.strip()  # https://thenextdoor.fr
                url = base_url + 'collections/all/products.json'
                # 发起Request请求访问网址url， 获取服务器响应的内容response并交给self.parse进行解析数据。
                yield Request(url=url, callback=self.parse, meta={'base_url': base_url})

    def parse(self, response):
        # 1. 将response响应的内容反序列化为python对象
        data = json.loads(response.text)
        # 2. 根据python数据类型解析商品的详情信息
        products = data.get('products')
        for product in products:
            item = ShopifyItem()
            item['title'] = product.get('title')
            item['image'] = product.get('images')[0].get('src')
            # https://thenextdoor.fr/products/golf-lefleur-gianno.json  商品名称.json
            item['link'] = response.meta['base_url'] + 'products/' + product.get('handle')
            item['price'] = product.get('variants')[0].get('price')
            # 发起request请求访问商品详情页url， 获取服务器的响应内容response并交给self.parse_detail处理。
            yield  Request(url=item['link'] + '.json', callback=self.parse_detail, meta={'item': copy.deepcopy(item)})

    def parse_detail(self, response):
        # 1. 将response响应的内容反序列化为python对象
        data = json.loads(response.text)
        # 2. 详情页获取2个信息: sizes={'S': {'sku': 'ccc', 'stock':'', 'price': 100}, 'M':{}}, stocks
        stocks = 0
        sizes = {}
        variants = data.get('product').get('variants')

        for variant in variants:
            # 如果网站不提供库存信息， 默认为1
            stock = variant.get('inventory_quantity', 1)
            # 如果对应尺寸存在，则存储相关信息
            if stock:
                size = variant.get('option1')
                sku = variant.get('sku')
                stocks += 1
                price = variant.get('price')
                sizes[size] = {'sku': sku, 'stock': stock, 'price':price}
        # 将尺寸信息序列化为json字符串
        item = response.meta['item']
        item['sizes'] = json.dumps(sizes)
        # 如果总库存存在(不等于0), 存储商品信息
        if stocks:
            item['stocks'] = stocks
            yield copy.deepcopy(item)





